<template>
  <el-card>
    <el-form :inline="true" :model="form">
      <el-form-item>
        <el-input
          v-model="form.order_no"
          :placeholder="$t('订单号')"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="form.status"
          :placeholder="$t('订单状态')"
          clearable
          class="w-36"
        >
          <el-option
            v-for="item in orderStatusList"
            :key="item.key"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="form.timeData"
          type="datetimerange"
          :range-separator="$t('至')"
          :start-placeholder="$t('开始日期')"
          :end-placeholder="$t('截止日期')"
          :default-time="pickerDefaultTime"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="onSubmit">
          {{ $t("查询") }}
        </el-button>
        <el-button type="info" @click="onExport">{{ $t("导出") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" v-loading="loading">
      <el-table-column
        prop="merchant_order_no"
        :label="$t('订单号')"
        align="center"
        width="260"
      />
      <el-table-column :label="$t('订单金额')" align="right">
        <template #default="{ row }">
          {{ numberToCurrencyNo(row.amount) }}
        </template>
      </el-table-column>
      <el-table-column prop="currency" :label="$t('币种')" align="center" />
      <el-table-column
        prop="pay_status_name"
        :label="$t('订单状态')"
        align="center"
      />
      <el-table-column
        prop="create_time"
        :label="$t('下单时间')"
        align="center"
        :formatter="formatterTime"
      />
      <el-table-column
        :label="$t('操作')"
        align="center"
        fixed="right"
        width="140"
      >
        <template #default="scope">
          <el-button
            round
            :icon="MoreFilled"
            class="text-[#e54c5f] cursor-pointer"
            v-btn="['all', 'web_acquiring_view']"
            @click="handleDetails(scope.row)"
            >{{ $t("查看") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="mt-5 flex justify-center">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageable.total"
        :default-page-size="pageable.pageSize"
        :default-current-page="pageable.pageNum"
        :page-sizes="[10, 25, 50, 100]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>
</template>
<script setup>
import { ref, reactive } from "vue";
import { ordersListApi } from "@/api/account/index";
import { Search, MoreFilled } from "@element-plus/icons-vue";
import {
  timestamp,
  formatterTime,
  numberToCurrencyNo,
  exportExcel,
  pickerDefaultTime,
} from "@/utils/utils";
const loading = ref(false);

import { useRouter } from "vue-router";
const router = useRouter();
const form = reactive({
  order_no: "",
  status: "",
  timeData: [],
});

const orderStatusList = ref([]);

//查看订单
const handleDetails = ({ ...row }) => {
  router.replace({
    path: "/acquiring/order/details",
    query: { order_no: row.order_no },
  });
};

// 分页
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
// 表格数据
const tableData = ref([]);
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    order_no: form.order_no,
    start_time: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end_time: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
    status: form.status,
  };
};
const onSubmit = () => {
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    // 获取数据
    const res = await ordersListApi(param());
    // console.log(res);
    loading.value = false;
    tableData.value = res.list;
    pageable.total = res.page.total_count;
    // console.log(pageable.total);
    orderStatusList.value = res.extra.filter_order_status.map((item) => {
      return { value: item.value, label: item.name };
    });
  } catch (error) {
    loading.value = false;
  }
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
  const res = await ordersListApi(data, "blob");
  exportExcel(res, "orders");
};
Promise.all([getTbaleData()]);
</script>
